﻿using System;
using System.Data;
using UniMana.Utils.Data;
using UniMana.Data;
using DevExpress.XtraEditors;
using System.Windows.Forms;

// Còn thiếu điều kiện năm học, học kỳ cho danh sách phòng

namespace UniMana.Win.Personnel
{
    public partial class FrmCheckSubjectClass_LecturerAll_AddSession : DevExpress.XtraEditors.XtraForm
    {
        string m_strPersonnalKey;
        Personnel_PersonnelInfo personinfo;
        Training_Subject_ClassInfo classinfo;

        int m_iClassKey;
        string m_strDegree;
        string m_strSubject;
        DateTime m_dDay;
        DateTime m_dDayModel;
        int m_iSession;

       //bool m_bFlagInsert = false;

        private DBMain m_data = new DBMain();

        public FrmCheckSubjectClass_LecturerAll_AddSession(string m_strPersonnalKey, int m_iClassKey, string m_strDegree, string m_strSubject, DateTime m_dDay, int m_iSession)
        {
            InitializeComponent();
            this.m_strPersonnalKey = m_strPersonnalKey;
            this.m_iClassKey = m_iClassKey;
            this.m_strDegree = m_strDegree;
            this.m_strSubject = m_strSubject;
            this.m_dDay = m_dDay;
            this.m_iSession = m_iSession;
        }

        private void FrmCheckSubjectClass_LecturerAll_AddSession_Load(object sender, EventArgs e)
        {
            personinfo = new Personnel_PersonnelInfo(m_strPersonnalKey);
            classinfo = new Training_Subject_ClassInfo(m_iClassKey);
            LoadData();
        }

        private void LoadData()
        {
            m_dDayModel = new DateTime();
            gluTraining_Session.GetData(m_data.m_DCReadOnly.Training_Sessions.GetDataKeyNameStarFinishTime(), "Training_SessionKey", "Name");
            // Còn thiếu điều kiện năm học, học kỳ cho danh sách phòng
            
            DataTable m_tblClassRom = new DataTable();
            m_tblClassRom = m_data.m_DCReadOnly.Training_Class_Rooms.GetDataWithSession(m_data.m_DCReadOnly.Training_Session_Subject_Class_Rooms, m_iSession).ToDataTable();
            DataView dv = m_tblClassRom.DefaultView;
            dv.Sort = "ID ASC";
            m_tblClassRom = dv.ToTable();

            gluTraining_Class_Room.GetData(m_tblClassRom,
                "Training_Class_RoomKey", "ID");
            txtPersonnel_PersonnelKey.Text = personinfo.Properties.ID;
            txtPersonnel_PersonnelName.Text = personinfo.Properties.LastName + " " + personinfo.Properties.Name;
            txtCatalogue_Degree.Text = m_strDegree;
            txtTraining_Subject.Text = m_strSubject;
            txtTraining_Subject_ClassID.Text = classinfo.Properties.ID;
            txtTraining_Subject_ClassName.Text = classinfo.Properties.Name;
            if (m_dDay > m_dDayModel)
            {
                dteDayTime.EditValue = (DateTime)m_dDay;
            }
            if (m_iSession > 0)
                gluTraining_Session.EditValue = m_iSession;
        }


        private bool CheckInfo()
        {
            try
            {
                if ((dteDayTime.EditValue == null) || (GetValueLookUpEdit.ConvertValueToInt(gluTraining_Class_Room.EditValue) <= 0) || (GetValueLookUpEdit.ConvertValueToInt(gluTraining_Session.EditValue) <= 0))
                    return false;
                return true;
            }
            catch { return false; }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (CheckInfo())
            {
                DataTable m_tbl = new DataTable();
                m_tbl = m_data.m_DCReadOnly.Training_Session_Subject_Class_Rooms.CheckInsert(int.Parse(gluTraining_Session.EditValue.ToString()),
                    int.Parse(gluTraining_Class_Room.EditValue.ToString()),
                    Convert.ToDateTime(dteDayTime.EditValue.ToString())).ToDataTable();
                if (m_tbl.Rows.Count > 0)
                {
                    DialogResult result = XtraMessageBox.Show("Thời điểm chọn đã có lớp đăng ký học. Bạn có muốn xem chi tiết?", "Thông báo", MessageBoxButtons.YesNo);
                    if (result == DialogResult.Yes)
                    {
                        FrmCheckSubjectClass_LecturerAll_AddSession_Detail detail = new FrmCheckSubjectClass_LecturerAll_AddSession_Detail(m_tbl);
                        detail.ShowDialog();
                    }
                }
                else
                {
                    //if (m_tbl.Rows[0]["Personnel_PersonnelKey"].ToString() == m_strPersonnalKey)
                    //{
                    m_tbl = m_data.m_DCReadOnly.Training_Session_Subject_Class_Rooms.CheckInsertOfPersonnal(m_strPersonnalKey, int.Parse(gluTraining_Session.EditValue.ToString()), Convert.ToDateTime(dteDayTime.EditValue.ToString())).ToDataTable();
                    if (m_tbl.Rows.Count > 0)
                        XtraMessageBox.Show("Ngày " + dteDayTime.Text + " - " + gluTraining_Session.Text + " của giảng viên " + txtPersonnel_PersonnelName.Text + " đã được đăng ký giảng.", "Thông báo");
                    else
                    {
                        Training_Session_Subject_Class_RoomInfo subclsinfo = new Training_Session_Subject_Class_RoomInfo();
                        subclsinfo.Properties.Training_SessionKey = int.Parse(gluTraining_Session.EditValue.ToString());
                        subclsinfo.Properties.Training_Subject_ClassKey = m_iClassKey;
                        subclsinfo.Properties.Training_Class_RoomKey = int.Parse(gluTraining_Class_Room.EditValue.ToString());
                        subclsinfo.Properties.Personnel_PersonnelKey = Guid.Parse(m_strPersonnalKey);
                        subclsinfo.Properties.RegistTime = Convert.ToDateTime(dteDayTime.EditValue);
                        subclsinfo.Properties.Training_WeekDayKey = ClsReturnDayOfWeek.convertToInt(Convert.ToDateTime(dteDayTime.EditValue));
                        subclsinfo.Properties.Note = rtbNote.Text;
                        subclsinfo.Save();
                        ClsPublic._flagLoadSession = true;
                        XtraMessageBox.Show("Đã lưu buổi giảng");
                    }
                    //}
                }
            }
            else
                XtraMessageBox.Show("Chưa có đầy đủ thông tin ngày, phòng, ca đăng ký giảng");            
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
